package com.wanghui.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanghui.reggie.dto.DishDto;
import com.wanghui.reggie.entity.Dish;
import com.wanghui.reggie.entity.DishFlavor;
import com.wanghui.reggie.mapper.DishMapper;
import com.wanghui.reggie.service.DishFlavorService;
import com.wanghui.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应的口味数据
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        //由于DishDto集合中封装的dishFlavor数据并没有dish_id数据，所以这里需要我们手动进行赋值
        //获取当前菜品的id
        Long dishId = dishDto.getId();
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //这里采用stream流的方式，先遍历集合，在对集合中的dish_id数据进行赋值
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //因为一个菜品可能对应多个口味数据，所以这里使用批量插入方法：saveBatch
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和对应口味信息
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdDishWithFlavor(Long id) {
        DishDto dishDto = new DishDto();
        //查询菜品信息，从dish表查询
        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish, dishDto); //拷贝基本属性到dishDto

        //查询口味信息，从dishFlavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors); //设置DishDto中flavors的值
        return dishDto;
    }

    /**
     * 更新Dish表信息，同时更新DishFlavor表信息
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateDishWithFlavor(DishDto dishDto) {
        //更新菜品表信息
        this.updateById(dishDto);

        //清理当前菜品对应的口味数据——dishFlavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //添加当前提交过来的口味数据——dishFlavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();    //获取flavors
        //为flavors添加dishId属性
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }
}
